/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import Beans.util.JsfUtil;
import Entidades.Address;
import Entidades.Client;
import SessionBeans.AddressFacade;
import SessionBeans.ClientFacade;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FlowEvent;

/**
 *
 * @author Pedro
 */
@ManagedBean(name = "clientWizardController")
@SessionScoped
public class ClientWizardController {

    private Client user= new Client();
    @EJB
    private ClientFacade ejbFacadeClient;
    private Address add=new Address();
    @EJB
    private AddressFacade ejbFacadeAdd;
    private static final Logger logger = Logger.getLogger(ClientWizardController.class.getName());

    public Client getUser() {
        return user;
    }

    public void setUser(Client user) {
        this.user = user;
    }

    public Address getAdd() {
        return add;
    }

    public void setAdd(Address add) {
        this.add = add;
    }

    public String save() {
        //Persist user  
       
        try {

            user.setAddress(add);
            ejbFacadeAdd.create(add);
            ejbFacadeClient.create(user);

            FacesMessage msg = new FacesMessage("Successful", "Welcome :" + user.getName());
            FacesContext.getCurrentInstance().addMessage(null, msg);
            user = new Client();
            add = new Address();
            return "/login";

        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return "/home";
        }
        
    }

    public String onFlowProcess(FlowEvent event) {
        logger.info("Current wizard step:" + event.getOldStep());
        logger.info("Next step:" + event.getNewStep());



        return event.getNewStep();
    }
}
